import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Collections;
import java.util.Properties;

public class Demo03KafkaConsumer {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "master:9092,node2:9092,node2:9092");
        properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        // 如果groupid之前消费过，则从上一次消费完之后开始
        // 如果groupid是新的，则以参数为准
        properties.setProperty("auto.offset.reset", "earliest");
        properties.setProperty("group.id", "Demo03KafkaConsumer02");

        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(properties);
        // 指定Topic
        consumer.subscribe(Collections.singleton("bigdata"));

        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(10000);

            for (ConsumerRecord<String, String> record : records) {
//                System.out.println(record.key());
                System.out.println(record.value());
            }
        }
    }
}
